package web.servlet;

import domain.Category;
import domain.Item;
import domain.Product;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import service.CatelogService;

import java.io.IOException;
import java.util.List;

public class ProductFormServlet extends HttpServlet {
    private CatelogService catelogService;
    private static final String PRODUCT_FORM="/WEB-INF/jsp/catalog/Product.jsp";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String productId=req.getParameter("productId");
        catelogService=new CatelogService();
        Product product=catelogService.getProduct(productId);
        List<Item> itemList=catelogService.getItemListByProduct(productId);
        Category category=catelogService.getCategory(product.getCategoryId());

        HttpSession session=req.getSession();
        session.setAttribute("product",product);
        session.setAttribute("itemList",itemList);
        session.setAttribute("category",category);
        req.getRequestDispatcher(PRODUCT_FORM).forward(req,resp);
    }
}
